package com.jgssp.ad.sdk;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdLoadType;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.jgssp.ad.config.TTAdManagerHolder;
import com.jgssp.ad.constant.AdConstant;
import com.jgssp.ad.utils.TToast;

/**
 * Created by wuzejian on 2020/3/11.
 */
public class RewardVideoActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "JgAds";

    private TextView mTvAdUnitId; //激励视频id
    private Button mBtLoadReward; //加载激励视频
    private Button mBtShowReward;//展示激励视频
    private RadioGroup radioGroup;

    private String mAdUnitId = AdConstant.REWARD_VOD; //横屏广告位id

    private boolean mLoadSuccess; //是否加载成功
    private boolean mIsLoadedAndShow;//广告加载成功并展示

    private TTAdNative mTTAdNative;
    private TTRewardVideoAd mTtRewardVideoAd;

    @SuppressLint("CutPasteId")
    @SuppressWarnings("RedundantCast")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reward_video);
        mTvAdUnitId = findViewById(R.id.tv_ad_unit_id);
        mBtLoadReward = findViewById(R.id.bt_load_reward);
        mBtShowReward = findViewById(R.id.bt_show_reward);
        radioGroup = findViewById(R.id.radio_group);

        mTTAdNative = TTAdManagerHolder.get().createAdNative(this);

        initRadioGroup();
        initListener();
        initAdLoader();
    }

    private void initRadioGroup() {

    }

    @Override
    public void initListener() {
        mBtLoadReward.setOnClickListener(this);
        mBtShowReward.setOnClickListener(this);
    }

    @Override
    public void initAdLoader() {
    }

    /**
     * 加载广告
     */
    private void loadAd(final String codeId) {
        //step5:创建广告请求参数AdSlot

        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId) // 广告代码位Id
                .setAdLoadType(TTAdLoadType.LOAD) // 本次广告用途：TTAdLoadType.LOAD实时；TTAdLoadType.PRELOAD预请求
                .setRewardAmount(123)
                .setRewardName("金币")
                .build();
        //step6:注册广告加载生命周期监听，请求广告

        mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int code, String msg) {
                TToast.show(RewardVideoActivity.this, msg + " code:" + code);
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                mTtRewardVideoAd = ttRewardVideoAd;
            }

            @Override
            public void onRewardVideoCached() {

            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                mTtRewardVideoAd = ttRewardVideoAd;
            }
        });
    }

    /**
     * 展示广告
     */
    private void showRewardAd() {
        if (mTtRewardVideoAd != null) {
            mTtRewardVideoAd.showRewardVideoAd(this);
        } else {
            TToast.show(this, "请先加载广告");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_load_reward: //加载激励视频
                mLoadSuccess = false;
                mIsLoadedAndShow = false;
                loadAd(mAdUnitId);
                break;
            case R.id.bt_show_reward: //展示激励视频
                showRewardAd();
                break;
        }
    }
}
